using System;
using System.Linq;
using System.Linq.Expressions;
using UnityEngine;
using QFramework;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;

namespace ProjectSurvival
{
    public partial class SimpleSword : ViewController
    {
        
        
        [FormerlySerializedAs("currentScenes")] [SerializeField]
        private float currentSecond = 1.5f;

        private void Update()
        {
            float superSwordAttackDuration = Global.SuperSword.Value ? 1 : 0.5f;
            currentSecond += Time.deltaTime;
            if (currentSecond > Global.AttackDuration.Value*superSwordAttackDuration)
            {
                currentSecond = 0f;

                int countTime = Global.SuperSword.Value ? 2 : 1;
                int damageTime = Global.SuperSword.Value ? Random.Range(2, 3) + 1 : 1;
                float distanceTime = Global.SuperSword.Value ? 2 : 1;
                var enemies = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None)
                    .OrderBy(enemy => enemy.Direction2DFrom(Player.Instance).magnitude)
                    .Where(e => e.Direction2DFrom(Player.Instance).magnitude <= Global.SimpleSwordRange.Value*distanceTime)
                    .Take(Global.SimpleSwordCount.Value+Global.AdditionalFlyThingCount.Value*countTime);
                //按距离排序，并按剑的数量值取敌人数量
                foreach (var enemy in enemies)
                {
                    Sword.Instantiate()
                        .Position(enemy.Position() + Vector3.left * 0.25f)
                        .Show()
                        .Self((self) =>
                        {
                            var selfCatch = self;
                            selfCatch.OnTriggerEnter2DEvent(other =>
                            {
                                var hurtBox = other.GetComponent<HitHurtBox>();
                                if (hurtBox)
                                {
                                    if (hurtBox.Owner.CompareTag("Enemy"))
                                    {
                                        //hurtBox.Owner.Hurt(Global.AbilityDamage.Value);
                                        DamageSystem.CalculateDamage(Global.AbilityDamage.Value*damageTime,hurtBox.Owner.GetComponent<Enemy>());
                                    }
                                }
                            }).UnRegisterWhenGameObjectDestroyed(gameObject);

                            //劈砍动画
                            ActionKit
                                .Sequence()
                                .Callback(() => { selfCatch.enabled = false; })
                                .Parallel(p =>
                                {
                                    p.Lerp(0, 10, 0.2f,
                                        rz => { selfCatch.LocalEulerAnglesZ(rz); });
                                    p.Append(ActionKit.Sequence()
                                        .Lerp(0, 1.25f, 0.1f, scale => selfCatch.LocalScale(scale))
                                        .Lerp(1.25f, 1f, 0.1f, scale => selfCatch.LocalScale(scale))
                                    );
                                })
                                .Callback(() => { selfCatch.enabled = true; })
                                .Parallel(p =>
                                {
                                    p.Lerp(10, -180, 0.2f, z => { selfCatch.LocalEulerAnglesZ(z); });
                                    p.Append(ActionKit.Sequence()
                                        .Lerp(1, 1.25f, 0.1f, scale => selfCatch.LocalScale(scale))
                                        .Lerp(1.25f, 1f, 0.1f, scale => selfCatch.LocalScale(scale))
                                    );
                                })
                                .Callback(() => { selfCatch.enabled = false; })
                                .Lerp(-180, 0, 0.3f, z =>
                                {
                                    selfCatch.LocalEulerAnglesZ(z);
                                    selfCatch.LocalScale(z.Abs() / 180);
                                })
                                .Start(this, () => { selfCatch.DestroyGameObjGracefully(); });
                        });
                }
            }
        }
    }
}